package jp.co.mclnet.util;

import java.io.Serializable;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;

import jp.co.mclnet.dao.Dao;
import jp.co.mclnet.dao.JdbcDaoImpl;
import jp.co.mclnet.dao.JdbcDaoImpl2;

public class ShimanukiCalc {
    private ExcelDao excelDao;
    private String filePath;
    private String tableName;
    private Connection conn;

    public static void main(String[] args) {
        Dao dao = new JdbcDaoImpl2();
        // じぇねりくす

        Iterator result = dao.read(new LinkedHashMap());

        List ary = new ArrayList();
        while (result.hasNext()) {
            ary.add(result.next());
        }

        // table名を取得
        List tableNames = new ArrayList();
        for (int i = 0; i < ary.size(); i++) {
            Object object = ary.get(i);
            Object z = Utils.getter(object, "z");
            if (!tableNames.contains(z)) {
                tableNames.add(z);
            }
            // setにして判定を除去
        }

        // テーブルとYをキーにし１レコード取得
        List columnNames = new ArrayList();
        for (int zi = 0; zi < tableNames.size(); zi++) {
            String tableName = (String) tableNames.get(zi);
            for (int i = 0; i < ary.size(); i++) {
                Object line = ary.get(i);
                Object y = Utils.getter(line, "y");
                if (Integer.valueOf(1).equals(y)) {
                    columnNames.add(Utils.getter(line, "value"));
                }
            }
        }
        /*
         * Connection conn = null; try { try {
         * Class.forName("com.mysql.jdbc.Driver"); } catch
         * (ClassNotFoundException e) { e.printStackTrace(); } try { String url
         * = "jdbc:mysql://localhost:3306/saturday?autoReconnect=true&amp;" +
         * "useUnicode=true&amp;characterEncoding=utf-8"; String user = "root";
         * String pass = ""; conn = DriverManager.getConnection(url, user,
         * pass); } catch (SQLException e) { e.printStackTrace(); }
         * ShimanukiCalc myself = new ShimanukiCalc(); myself.setExcelDao(new
         * ExcelDaoShimanuki()); myself.setFilePath("deprecated/xls/speed.xls");
         * myself.setTableName("personal"); myself.setConnection(conn);
         * myself.execute(new LinkedHashMap<Object, Object>()); try {
         * conn.commit(); } catch (SQLException e) { e.printStackTrace(); } }
         * finally { if (conn != null) { try { conn.close(); } catch
         * (SQLException e) { e.printStackTrace(); } } }
         */

    }

    public void setConnection(Connection conn) {
        this.conn = conn;
    }

    public <E extends Serializable> Object execute(E obj) {
        ExcelDao excelDao = getExcelDao();
        excelDao.setFilePath(getFilePath());
        excelDao.setTableName(getTableName());

        Iterator<E> result = excelDao.read(obj);

        // excelの結果を使ってdaoにcreateする
        JdbcDaoImpl dao = new JdbcDaoImpl(getTableName());
        dao.setConnection(getConn());

        while (result.hasNext()) {
            E next = result.next();
            dao.create(next);
        }

        return null;
    }

    public ExcelDao getExcelDao() {
        return excelDao;
    }

    public String getFilePath() {
        return filePath;
    }

    public String getTableName() {
        return tableName;
    }

    public void setExcelDao(ExcelDao excelDao) {
        this.excelDao = excelDao;
    }

    public void setFilePath(String path) {
        this.filePath = path;
    }

    public void setTableName(String table) {
        this.tableName = table;
    }

    public Connection getConn() {
        return conn;
    }

    public void setConn(Connection conn) {
        this.conn = conn;
    }

}
